package com.yan;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;

/**
 * TODO
 *
 * @Author: 汪焰
 * @Date: 2021/7/5 23:32
 */
@Configuration
public class RabbitConfig {

    //连接工程
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory(URI.create("amqp://root:root@1.116.195.238:5672/%2f"));
        return factory;
    }

    //RabbitTemplate
    @Bean
    @Autowired
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
        return rabbitTemplate;
    }

    //RabbitAdmin
    @Bean
    @Autowired
    public RabbitAdmin rabbitAdmin(ConnectionFactory factory) {
        RabbitAdmin rabbitTemplate = new RabbitAdmin(factory);
        return rabbitTemplate;
    }

    //Queue
    @Bean
    public Queue queue() {
        Queue queue = QueueBuilder.nonDurable("queue.anno.name.yan").build();
        return queue;
    }

    //Exchange
    @Bean
    public Exchange exchange() {
        FanoutExchange fanoutExchange = new FanoutExchange("ex.anno.fanout.name.yan", false, false, null);
        return fanoutExchange;
    }

    //Binding
    @Bean
    @Autowired
    public Binding binding(Queue queue, Exchange exchange) {
        //创建一个绑定，不知道绑定的参数
        Binding binding = BindingBuilder.bind(queue).to(exchange).with("key.anno.name.yan").noargs();
        return binding;
    }


}
